<?php

/**
 * @Author: Wang Chunsheng 2192138785@qq.com
 * @Date:   2020-03-02 08:38:14
 * @Last Modified by:   Wang chunsheng  email:2192138785@qq.com
 * @Last Modified time: 2020-06-01 15:43:25
 */

namespace common\addons\diandi_shop\models;

use common\addons\diandi_shop\models\enums\GoodsStatus;
use diandi\addons\behaviors\BlocBehavior;
use Yii;
use yii\helpers\StringHelper;

/**
 * This is the model class for table "dd_goods".
 *
 * @property int    $goods_id
 * @property string $goods_name
 * @property int    $category_id
 * @property int    $spec_type
 * @property int    $deduct_stock_type
 * @property string $content
 * @property int    $sales_initial
 * @property int    $sales_actual
 * @property int    $goods_sort
 * @property int    $delivery_id
 * @property int    $goods_status
 * @property int    $is_delete
 * @property int    $wxapp_id
 * @property int    $create_time
 * @property int    $update_time
 */
class DdGoods extends \yii\db\ActiveRecord
{
    
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%diandi_shop_goods}}';
    }

    public $spec_item_thumb;

    /**
     * 行为.
     */
    public function behaviors()
    {
        /*自动添加创建和修改时间*/
        return [
            [
                'class' => \common\behaviors\SaveBehavior::className(),
                'updatedAttribute' => 'update_time',
                'createdAttribute' => 'create_time',
            ],
        ];
    }

    public function beforeValidate()
    {
        if (parent::beforeValidate()) {
            if (!is_numeric($this->goods_status) && isset($this->goods_status)) {
                //字段
                $this->goods_status = GoodsStatus::getValueByName($this->goods_status);
            }

            return true;
        } else {
            return false;
        }
    }

    public function getSpecItemThumb()
    {
        return $this->spec_item_thumb;
    }

    public function setSpecItemThumb($spec_item_thumb)
    {
        $this->spec_item_thumb = $spec_item_thumb;
    }

    public function afterFind()
    {
        parent::afterFind();
        
        
    }
    
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['category_id', 'spec_type', 'deduct_stock_type', 'sales_initial', 'goods_sort', 'delivery_id', 'goods_status', 'create_time', 'update_time','bloc_id','store_id'], 'integer'],
            [['content', 'images', 'thumb', 'goods_name', 'category_id', 'sales_initial', 'category_pid', 'stock', 'goods_weight'], 'required'],
            [['content', 'images'], 'compare', 'compareValue' => '', 'operator' => '!='],
            [['goods_name', 'label', 'video'], 'string', 'max' => 255],
            ['stock', 'compare', 'compareValue' => 0, 'operator' => '>='],
            [['line_price', 'goods_price', 'browse', 'goods_weight', 'delivery_id'], 'number'],
            [['category_id'], 'compare', 'compareValue' => 0, 'operator' => '!='],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'goods_id' => '商品ID',
            'goods_name' => '商品名称',
            'bloc_id'=>'公司ID',
            'store_id'=>'商户ID',
            'video' => '短视频',
            'category_id' => '分类',
            'label' => '商品标签',
            'spec_type' => '商品规格',
            'goods_weight' => '重量(克)',
            'deduct_stock_type' => '库存计算方式',
            'content' => '商品介绍',
            'sales_initial' => '初始销量',
            'sales_actual' => 'Sales Actual',
            'goods_sort' => '商品排序',
            'delivery_id' => '运费模板',
            'goods_status' => '是否上架',
            'is_delete' => '是否删除',
            'wxapp_id' => 'Wxapp ID',
            'create_time' => '添加时间',
            'update_time' => '更新时间',
            'images' => '商品相册',
            'thumb' => '商品主图',
            'line_price' => '市场价',
            'sales_actual' => '实际销量',
            'browse' => '浏览量',
            'goods_price' => '销售价格',
            'stock' => '库存',
            'spec_item_thumb' => '属性图片',
        ];
    }

    /* 获取分类 */
    public function getCategory()
    {
        return $this->hasOne(DdCategory::className(), ['category_id' => 'category_id']);
    }

    /**
     * 获取商品规格.
     *
     * @param int|null post
     *
     * @return string
     *
     * @throws NotFoundHttpException
     */
    public function getSpec()
    {
        return $this->hasMany(DdSpec::className(), ['goods_id' => 'goods_id']);
    }

    /**
     * 获取商品规格.
     *
     * @param int|null post
     *
     * @return string
     *
     * @throws NotFoundHttpException
     */
    public function getGoodsSpec()
    {
        return $this->hasMany(DdGoodsSpec::className(), ['goods_id' => 'goods_id']);
    }

    /**
     * 获取属性关联关系.
     *
     * @param int|null post
     *
     * @return string
     *
     * @throws NotFoundHttpException
     */
    public function getSpecRel()
    {
        return $this->hasMany(DdGoodsSpecRel::className(), ['goods_id' => 'goods_id']);
    }

    /**
     * 报错商品的规格.
     *
     * @param int|null post
     *
     * @return string
     *
     * @throws NotFoundHttpException
     */
    public function saveSpec($goods_id, $specs = [])
    {
        // 获取组合id
        $option_ids = $specs['option_ids'];
        // 规格名称
        $spec_titles = $specs['spec_title'];
        // 规格类型
        $spec_str_ids = $specs['spec_id'];
        // 规格名称
        $spec_item_titles = $specs['spec_item_title'];
        // 属性值
        $params = $specs['param'];
        // 规格显示
        $spec_item_shows = $specs['spec_item_show'];
        // 规格图片
        $spec_item_thumbs = $specs['DdGoods']['spec_item_thumb'];

        // 全局规格项
        $DdSpec = new DdSpec();
        // 全局规格值
        $DdSpecValue = new DdSpecValue();
        // 商品规格关系表
        $DdGoodsSpecRel = new DdGoodsSpecRel();
        // 删除商品已有的
        $DdGoodsSpecRel::deleteAll(['goods_id' => $goods_id]);

        // 保存规格类型
        if (!empty($spec_titles)) {
            foreach ($spec_titles as $key => $value) {
                $_DdSpec = clone $DdSpec;
                $_DdSpec->spec_name = $value;
                $_DdSpec->create_time = time();
                $spec_have_id = $_DdSpec::find()->where(['spec_name' => $value])->select('spec_id')->one();
                if (!empty($spec_have_id)) {
                    // 存在直接使用
                    $spec_ids[$key] = $spec_have_id->spec_id;
                } else {
                    // 不存在保存使用
                    $_DdSpec->save();
                    $spec_ids[$key] = Yii::$app->db->getLastInsertID();
                }
                if (!empty($spec_item_titles[$key])) {
                    // 保存规格值
                    foreach ($spec_item_titles[$key] as $k => $val) {
                        $_DdSpecValue = clone $DdSpecValue;
                        // 查询规格是否值是否存在
                        $spec_value_have_ids = $_DdSpecValue::find()
                            ->where(['spec_id' => $spec_ids[$key], 'spec_value' => $val])
                            ->select('spec_value_id')
                            ->one();
                        if (!empty($spec_value_have_ids)) {
                            // 规格值存在直接使用
                            $spec_value_ids[$k] = $spec_value_have_ids->spec_value_id;
                        } else {
                            // 不存在保存使用
                            $_DdSpecValue->spec_value = $val;
                            $_DdSpecValue->spec_id = $spec_ids[$key];
                            $_DdSpecValue->create_time = time();
                            $_DdSpecValue->save();
                            $spec_value_ids[$k] = Yii::$app->db->getLastInsertID();
                        }
                        /* 写入属性与商品关联关系表 */
                        $_DdGoodsSpecRel = clone $DdGoodsSpecRel;
                        $_DdGoodsSpecRel->setAttributes([
                            'goods_id' => $goods_id,
                            'spec_id' => $spec_ids[$key],
                            'spec_item_show' => $spec_item_shows[$key][$k],
                            'thumb' => $spec_item_thumbs[$key][$k],
                            'spec_value_id' => $spec_value_ids[$k],
                            'create_time' => time(),
                        ]);
                        $_DdGoodsSpecRel->save();
                        $spec_sku_id[$key][$k] = Yii::$app->db->getLastInsertID();
                    }
                }
            }
        }

        /* 存储规格属性值 */

        $DdGoodsSpec = new DdGoodsSpec();
        if (!empty($option_ids)) {
            $DdGoodsSpec::deleteAll(['goods_id' => $goods_id]);
            // 根据组合id写入商品属性值
            $stock_num = 0;
            foreach ($option_ids as $key => $value) {
                $list = StringHelper::explode($value, '_');
                // 写入商品属性值
                $_DdGoodsSpec = clone $DdGoodsSpec;
                foreach ($list as $item) {
                    $idss[$key][] = $spec_value_ids[$item];
                }

                $_DdGoodsSpec->setAttributes(
                    [
                        'goods_id' => $goods_id,
                        'goods_no' => '123',
                        'goods_price' => $specs['option_marketprice_'.$value][0],
                        'line_price' => $specs['option_productprice_'.$value][0],
                        'stock_num' => $specs['option_stock_'.$value][0],
                        'goods_costprice' => $specs['option_costprice_'.$value][0],
                        'goods_weight' => $specs['option_weight_'.$value][0],
                        'spec_sku_id' => implode('_', $idss[$key]),
                        'create_time' => time(),
                    ]
                );
                $stock_num += $specs['option_stock_'.$value][0];
                $res[] = $_DdGoodsSpec->save();
            }
            $this->updateAll(['stock' => $stock_num], ['goods_id' => $goods_id]);
        }
        // 存储属性
        if (!empty($params)) {
            $DdGoodsParam = new DdGoodsParam();

            $param_id = $params['param_id'];
            foreach ($param_id as $item) {
                $_DdGoodsParam = clone $DdGoodsParam;

                if (is_numeric($item)) {
                    $_DdGoodsParam->updateAll([
                        'title' => $params['param_title'][$item],
                        'goods_id' => $goods_id,
                        'value' => $params['param_value'][$item],
                    ], ['id' => $item]);
                } else {
                    $_DdGoodsParam->setAttributes([
                        'title' => $params['param_title'][$item],
                        'goods_id' => $goods_id,
                        'value' => $params['param_value'][$item],
                    ]);
                    $_DdGoodsParam->save();
                }
                $_DdGoodsParam->save();
            }
        }
    }

    public function createHtml($options, $allspecs)
    {
        print_r($options);
        print_r($allspecs);
        exit;
        if (count($options) > 0) {
            $specitemids = explode('_', $options[0]['specs']);
            foreach ($specitemids as $itemid) {
                foreach ($allspecs as $ss) {
                    $items = $ss['items'];
                    foreach ($items as $it) {
                        if ($it['id'] == $itemid) {
                            $specs[] = $ss;
                            break;
                        }
                    }
                }
            }
            $html = '';
            $html .= '<table class="table table-bordered table-condensed">';
            $html .= '<thead>';
            $html .= '<tr class="active">';
            $len = count($specs);
            $newlen = 1; //多少种组合
            $h = array(); //显示表格二维数组
            $rowspans = array(); //每个列的rowspan
            for ($i = 0; $i < $len; ++$i) {
                //表头
                $html .= "<th style='width:80px;'>".$specs[$i]['title'].'</th>';
                //计算多种组合
                $itemlen = count($specs[$i]['items']);
                if ($itemlen <= 0) {
                    $itemlen = 1;
                }
                $newlen *= $itemlen;
                //初始化 二维数组
                $h = array();
                for ($j = 0; $j < $newlen; ++$j) {
                    $h[$i][$j] = array();
                }
                //计算rowspan
                $l = count($specs[$i]['items']);
                $rowspans[$i] = 1;
                for ($j = $i + 1; $j < $len; ++$j) {
                    $rowspans[$i] *= count($specs[$j]['items']);
                }
            }
            $html .= '<th class="info" style="width:130px;"><div class=""><div style="padding-bottom:10px;text-align:center;font-size:16px;">库存</div><div class="input-group"><input type="text" class="form-control option_stock_all"  VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-hand-o-down" title="批量设置" onclick="setCol(\'option_stock\');"></a></span></div></div></th>';
            $html .= '<th class="success" style="width:150px;"><div class=""><div style="padding-bottom:10px;text-align:center;font-size:16px;">销售价格</div><div class="input-group"><input type="text" class="form-control option_marketprice_all"  VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-hand-o-down" title="批量设置" onclick="setCol(\'option_marketprice\');"></a></span></div></div></th>';
            $html .= '<th class="warning" style="width:150px;"><div class=""><div style="padding-bottom:10px;text-align:center;font-size:16px;">市场价格</div><div class="input-group"><input type="text" class="form-control option_productprice_all"  VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-hand-o-down" title="批量设置" onclick="setCol(\'option_productprice\');"></a></span></div></div></th>';
            $html .= '<th class="danger" style="width:150px;"><div class=""><div style="padding-bottom:10px;text-align:center;font-size:16px;">成本价格</div><div class="input-group"><input type="text" class="form-control option_costprice_all"  VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-hand-o-down" title="批量设置" onclick="setCol(\'option_costprice\');"></a></span></div></div></th>';
            $html .= '<th class="info" style="width:150px;"><div class=""><div style="padding-bottom:10px;text-align:center;font-size:16px;">重量（克）</div><div class="input-group"><input type="text" class="form-control option_weight_all"  VALUE=""/><span class="input-group-addon"><a href="javascript:;" class="fa fa-hand-o-down" title="批量设置" onclick="setCol(\'option_weight\');"></a></span></div></div></th>';
            $html .= '</tr></thead>';
            for ($m = 0; $m < $len; ++$m) {
                $k = 0;
                $kid = 0;
                $n = 0;
                for ($j = 0; $j < $newlen; ++$j) {
                    $rowspan = $rowspans[$m];
                    if ($j % $rowspan == 0) {
                        $h[$m][$j] = array('html' => "<td rowspan='".$rowspan."'>".$specs[$m]['items'][$kid]['title'].'</td>', 'id' => $specs[$m]['items'][$kid]['id']);
                    } else {
                        $h[$m][$j] = array('html' => '', 'id' => $specs[$m]['items'][$kid]['id']);
                    }
                    ++$n;
                    if ($n == $rowspan) {
                        ++$kid;
                        if ($kid > count($specs[$m]['items']) - 1) {
                            $kid = 0;
                        }
                        $n = 0;
                    }
                }
            }
            $hh = '';
            for ($i = 0; $i < $newlen; ++$i) {
                $hh .= '<tr>';
                $ids = array();
                for ($j = 0; $j < $len; ++$j) {
                    $hh .= $h[$j][$i]['html'];
                    $ids[] = $h[$j][$i]['id'];
                }
                $ids = implode('_', $ids);
                $val = array('id' => '', 'title' => '', 'stock' => '', 'costprice' => '', 'productprice' => '', 'marketprice' => '', 'weight' => '');
                foreach ($options as $o) {
                    if ($ids === $o['specs']) {
                        $val = array(
                            'id' => $o['id'],
                            'title' => $o['title'],
                            'stock' => $o['stock'],
                            'costprice' => $o['costprice'],
                            'productprice' => $o['productprice'],
                            'marketprice' => $o['marketprice'],
                            'weight' => $o['weight'],
                        );
                        break;
                    }
                }
                $hh .= '<td class="info">';
                $hh .= '<input name="option_stock_'.$ids.'[]"  type="text" class="form-control option_stock option_stock_'.$ids.'" value="'.$val['stock'].'"/></td>';
                $hh .= '<input name="option_id_'.$ids.'[]"  type="hidden" class="form-control option_id option_id_'.$ids.'" value="'.$val['id'].'"/>';
                $hh .= '<input name="option_ids[]"  type="hidden" class="form-control option_ids option_ids_'.$ids.'" value="'.$ids.'"/>';
                $hh .= '<input name="option_title_'.$ids.'[]"  type="hidden" class="form-control option_title option_title_'.$ids.'" value="'.$val['title'].'"/>';
                $hh .= '</td>';
                $hh .= '<td class="success"><input name="option_marketprice_'.$ids.'[]" type="text" class="form-control option_marketprice option_marketprice_'.$ids.'" value="'.$val['marketprice'].'"/></td>';
                $hh .= '<td class="warning"><input name="option_productprice_'.$ids.'[]" type="text" class="form-control option_productprice option_productprice_'.$ids.'" " value="'.$val['productprice'].'"/></td>';
                $hh .= '<td class="danger"><input name="option_costprice_'.$ids.'[]" type="text" class="form-control option_costprice option_costprice_'.$ids.'" " value="'.$val['costprice'].'"/></td>';
                $hh .= '<td class="info"><input name="option_weight_'.$ids.'[]" type="text" class="form-control option_weight option_weight_'.$ids.'" " value="'.$val['weight'].'"/></td>';
                $hh .= '</tr>';
            }
            $html .= $hh;
            $html .= '</table>';
        }

        return $html;
    }
}
